package com.wenhuitec.sysui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.sysui.R;
import com.wenhuitec.sysui.dialog.ShowKuCunDialog;
import com.wenhuitec.sysui.entity.FoodSkuModel;

import java.util.List;


public class AddFoodSkuAdapter extends RecyclerView.Adapter<AddFoodSkuAdapter.MyViewHolder> {


    private LayoutInflater inflater;
    private Context mContext;
    private List<FoodSkuModel> mFoodSkuModelList;

    public AddFoodSkuAdapter(Context context, List<FoodSkuModel> foodSkuModelList) {
        inflater = LayoutInflater.from(context);
        mContext = context;
        mFoodSkuModelList = foodSkuModelList;

    }

    public List<FoodSkuModel> getmFoodSkuModelList() {
        return mFoodSkuModelList;
    }


    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public AddFoodSkuAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_sku, null);
        return new MyViewHolder(itemView);
    }


    //给每一行View填充数据
    @Override
    public void onBindViewHolder(AddFoodSkuAdapter.MyViewHolder holder, int position) {

        //region 添加按钮点击
        holder.addSkuBut.setOnClickListener((v -> {
            mFoodSkuModelList.add(new FoodSkuModel());
            notifyDataSetChanged();
        }));
        //endregion

        //region 删除按钮点击
        holder.delSkuBut.setOnClickListener((v -> {

            mFoodSkuModelList.remove(position);
            notifyDataSetChanged();
        }));
        //endregion

        //region 库存按钮点击
        holder.kuCunTR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowKuCunDialog showKuCunDialog = new ShowKuCunDialog();
                showKuCunDialog.BottomDialog(mContext,kuCunOnSave);
                showKuCunDialog.setDate(mFoodSkuModelList.get(position).getStock(), mFoodSkuModelList.get(position).getMaxStock());
            }
        });
        //endregion


        holder.skuNameEd.setText(mFoodSkuModelList.get(position).getName());//商品名称
        holder.skuPriceEd.setText(mFoodSkuModelList.get(position).getPrice());//商品价格
        holder.packingChargeEd.setText(mFoodSkuModelList.get(position).getPachagePrice());//包装费

        //region 规格名输入框变化监听器
        holder.skuNameEd.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            try {
                if (!hasFocus) {
                    EditText _v = (EditText) v;
//                    System.out.println(_v.getText().toString());
                    mFoodSkuModelList.get(position).setName(_v.getText().toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        //endregion
        //region 价格输入框变化监听器
        holder.skuPriceEd.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            try {
                if (!hasFocus) {
                    EditText _v = (EditText) v;
//                    System.out.println(_v.getText().toString());
                    mFoodSkuModelList.get(position).setPrice(_v.getText().toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        //endregion
        //region 包装费输入框变化监听器
        holder.packingChargeEd.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            try {
                if (!hasFocus) {
                    EditText _v = (EditText) v;
                    mFoodSkuModelList.get(position).setPachagePrice(_v.getText().toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        //endregion


        //region 新增按钮的显示和隐藏
        if ((mFoodSkuModelList.size() - 1) == position) {
            holder.addSkuBut.setVisibility(View.VISIBLE);
        } else {
            holder.addSkuBut.setVisibility(View.GONE);
        }
        //endregion
        //region 删除按钮的显示和隐藏
        if (mFoodSkuModelList.size() == 1) {
            holder.delSkuBut.setVisibility(View.GONE);
        } else {
            holder.delSkuBut.setVisibility(View.VISIBLE);
        }
        //endregion

    }

    @Override
    public int getItemCount() {
        return mFoodSkuModelList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView addSkuBut;
        TextView delSkuBut;
        EditText skuNameEd;
        EditText skuPriceEd;
        TableRow kuCunTR;
        EditText packingChargeEd;

        public MyViewHolder(View itemView) {
            super(itemView);
            addSkuBut = (TextView) itemView.findViewById(R.id.addSkuBut);
            delSkuBut = (TextView) itemView.findViewById(R.id.delSkuBut);
            skuNameEd = (EditText) itemView.findViewById(R.id.skuNameEd);
            skuPriceEd = (EditText) itemView.findViewById(R.id.skuPriceEd);
            kuCunTR = (TableRow) itemView.findViewById(R.id.kuCunTR);
            packingChargeEd = (EditText) itemView.findViewById(R.id.packingChargeEd);
        }
    }

    //region 库存弹窗时的保存点击返回
    ShowKuCunDialog.KuCunOnSave kuCunOnSave=new ShowKuCunDialog.KuCunOnSave() {
        @Override
        public void save(String stock, String maxStock, int autoStock) {

        }
    };
    //endregion
}
